﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;

namespace YJKArch.AxisModule
{
	internal class GridXdataManger
	{
		private static Schema CreateSimpleSchema()
		{
			SchemaBuilder schemaBuilder = new SchemaBuilder(GridXdataManger.m_Guid);
			schemaBuilder.SetReadAccessLevel((AccessLevel)1);
			schemaBuilder.SetSchemaName(GridXdataManger.m_SchemaName);
			schemaBuilder.AddArrayField(GridXdataManger.m_strBubbleStatus, typeof(int));
			schemaBuilder.AddArrayField(GridXdataManger.m_strHasBubble, typeof(int));
			schemaBuilder.AddArrayField(GridXdataManger.m_ViewId, typeof(ElementId));
			return schemaBuilder.Finish();
		}

		private static bool HasSchema(out Schema schema)
		{
			Schema schema2 = Schema.Lookup(GridXdataManger.m_Guid);
			schema = schema2;
			return schema2 != null;
		}

		public static void WriteBubbleStatus(Element element, List<GridStatusEachView> BubbleStatus)
		{
			Schema schema = null;
			if (!GridXdataManger.HasSchema(out schema))
			{
				schema = GridXdataManger.CreateSimpleSchema();
			}
			if (GridXdataManger.m_GridEnt == null)
			{
				GridXdataManger.m_GridEnt = new Entity(schema);
			}
			List<ElementId> list = new List<ElementId>();
			List<int> list2 = new List<int>();
			List<int> list3 = new List<int>();
			foreach (GridStatusEachView gridStatusEachView in BubbleStatus)
			{
				list.Add(gridStatusEachView.ViewID);
				list2.Add(gridStatusEachView.nStatus);
				list3.Add(gridStatusEachView.nHasTag);
			}
			Field field = schema.GetField(GridXdataManger.m_ViewId);
			GridXdataManger.m_GridEnt.Set<IList<ElementId>>(field, list);
			Field field2 = schema.GetField(GridXdataManger.m_strBubbleStatus);
			GridXdataManger.m_GridEnt.Set<IList<int>>(field2, list2);
			Field field3 = schema.GetField(GridXdataManger.m_strHasBubble);
			GridXdataManger.m_GridEnt.Set<IList<int>>(field3, list3);
			element.SetEntity(GridXdataManger.m_GridEnt);
			GridXdataManger.m_GridEnt = null;
		}

		public static bool ReadBubbleStatus(Element element, ref List<GridStatusEachView> BubbleStatus)
		{
			Schema schema = null;
			if (GridXdataManger.HasSchema(out schema))
			{
				Entity entity = element.GetEntity(schema);
				if (!entity.IsValid())
				{
					return false;
				}
				List<int> list = new List<int>();
				Field field = schema.GetField(GridXdataManger.m_strBubbleStatus);
				if (field != null)
				{
					list = (entity.Get<IList<int>>(field) as List<int>);
				}
				List<int> list2 = new List<int>();
				Field field2 = schema.GetField(GridXdataManger.m_strHasBubble);
				if (field2 != null)
				{
					list2 = (entity.Get<IList<int>>(field2) as List<int>);
				}
				List<ElementId> list3 = new List<ElementId>();
				Field field3 = schema.GetField(GridXdataManger.m_ViewId);
				if (field3 != null)
				{
					list3 = (entity.Get<IList<ElementId>>(field3) as List<ElementId>);
				}
				if (list3.Count != 0 && list3.Count == list.Count && list2.Count != 0 && list3.Count == list2.Count)
				{
					for (int i = 0; i < list3.Count; i++)
					{
						GridStatusEachView item = default(GridStatusEachView);
						item.nStatus = list[i];
						item.ViewID = list3[i];
						item.nHasTag = list2[i];
						BubbleStatus.Add(item);
					}
					return true;
				}
			}
			return false;
		}

		public static bool DeleteBubblesEntity(Element element)
		{
			Schema schema = null;
			return GridXdataManger.HasSchema(out schema) && element.DeleteEntity(schema);
		}

		public static Guid m_Guid = new Guid("F0D995F4-A4B0-4CD1-8316-179F99CDED31");

		private static string m_SchemaName = "BubbleStatusForEachView";

		private static string m_ViewId = "ViewId";

		private static string m_strBubbleStatus = "BubbleStatus";

		private static string m_strHasBubble = "HasBubble";

		public static Entity m_GridEnt = null;
	}
}
